package com.zrf.testchatgpt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zrf.testchatgpt.constant.CacheConstants;
import com.zrf.testchatgpt.entity.Permission;
import com.zrf.testchatgpt.entity.Role;
import com.zrf.testchatgpt.entity.vo.PermissionVo;
import com.zrf.testchatgpt.entity.vo.RoleVo;
import com.zrf.testchatgpt.mapper.PermissionMapper;
import com.zrf.testchatgpt.mapper.RoleMapper;
import com.zrf.testchatgpt.service.impl.PermissionServiceImpl;
import com.zrf.testchatgpt.service.impl.RoleServiceImpl;
import com.zrf.testchatgpt.shiro.util.ShiroUtils;
import com.zrf.testchatgpt.util.RedisCacheUtil;
import com.zrf.testchatgpt.util.Result;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.cache.Cache;
import org.crazycake.shiro.RedisCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("auth")
@RequiresRoles("admin")
public class RoleAndPermissionController {
    @Autowired
    RoleServiceImpl roleService;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    PermissionServiceImpl permissionService;
    @Autowired
    ShiroUtils shiroUtils;
//    @Autowired
//    RedisCacheUtil redisCacheUtil;
    @PutMapping("updateRoleById/{id}")
    public Result updateRoleById(@PathVariable("id") String userId, @RequestBody RoleVo roleVo){
        List<Integer> permissionByRoleId = roleMapper.getRoleByUserId(roleVo.getId());
        List<Integer> routes = roleVo.getRoles();
        List<Integer> inserts = routes.stream().filter(item -> !permissionByRoleId.contains(item)).collect(Collectors.toList());
        List<Integer> deletes = permissionByRoleId.stream().filter(item -> !routes.contains(item)).collect(Collectors.toList());
        if (inserts.size()>0)
            roleService.updateRoleById(userId,inserts);
        if (deletes.size()>0)
            roleService.deleteRoleById(userId,deletes);
          shiroUtils.deleteAuth(userId);
        return Result.builder().code("200").msg("成功").build();
    }
    @PutMapping("updatePermissionById/{id}")
    public Result updatePermissionById(@PathVariable("id") String userId,@RequestBody PermissionVo permissionVo){
        List<Integer> permissionByRoleId = permissionMapper.getPermissionByUserId(permissionVo.getId());
        List<Integer> routes = permissionVo.getPermissions();
        List<Integer> inserts = routes.stream().filter(item -> !permissionByRoleId.contains(item)).collect(Collectors.toList());
        List<Integer> deletes = permissionByRoleId.stream().filter(item -> !routes.contains(item)).collect(Collectors.toList());
        if (inserts.size()>0)
        permissionService.updatePermissionById(userId,inserts);
        if (deletes.size()>0)
            permissionService.deletePermissionById(userId,deletes);
        shiroUtils.deleteAuth(userId);
        return Result.builder().code("200").msg("成功").build();
    }
//    @PostMapping("deletePermissionById")
//    public Result deletePermissionById(String userId,String permissionId){
//        permissionService.deletePermissionById(userId,permissionId);
//        shiroUtils.deleteCache(userId,true);
//        return Result.builder().code("200").msg("成功").build();
//    }
//    @PostMapping("deleteRoleById")
//    public Result deleteRoleById(String userId,String roleId){
//        roleService.deleteRoleById(userId,roleId);
//        shiroUtils.deleteCache(userId,true);
//        return Result.builder().code("200").msg("成功").build();
//    }

    @GetMapping("getroles")
    public Result getAllRoles(){
        LambdaQueryWrapper<Role>  wrapper=new LambdaQueryWrapper<>();
        List<Role> roles = roleMapper.selectList(wrapper);
        return  Result.builder().code("200").data(roles).build();
    }

    @GetMapping("getpermission")
    public Result getpermission(){
        LambdaQueryWrapper<Permission>  wrapper=new LambdaQueryWrapper<>();
        List<Permission> roles = permissionMapper.selectList(wrapper);
        return  Result.builder().code("200").data(roles).build();
    }


    @PutMapping("updateRole/{id}")
    public Result updateRole(@PathVariable("id")String roleId,@RequestBody Role role){

        List<Integer> permissionByRoleId = permissionMapper.getPermissionByRoleId(role.getId().toString());
        List<Integer> routes = role.getRoutes();
        List<Integer> inserts = routes.stream().filter(item -> !permissionByRoleId.contains(item)).collect(Collectors.toList());
        List<Integer> deletes = permissionByRoleId.stream().filter(item -> !routes.contains(item)).collect(Collectors.toList());
        if (deletes.size()>0)
        roleService.deleteRoleP(role.getId().toString(),deletes);
        if (inserts.size()>0)
        roleService.insertRole(role.getId().toString(),inserts);
        return Result.builder().code("200").msg("权限更新成功").build();
    }
    @PostMapping("addRole")
    public Result addRole(@RequestBody Role role){
        if (role==null){
            return Result.builder().code("500").msg("添加失败").build();
        }
        roleMapper.Myinsert(role);
        roleService.insertRole(role.getId().toString(),role.getRoutes());
        return Result.builder().code("200").msg("添加成功").data(role.getId()).build();
    }
    @DeleteMapping("deleteRole/{id}")
    public Result deleteRole(@PathVariable("id")String roleId){
        LambdaQueryWrapper<Role> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Role::getId,roleId);
        roleMapper.delete(wrapper);
        return Result.builder().code("200").msg("删除成功").build();
    }
    @GetMapping("getRoleByUserId")
    public Result getRoleByUserId(String userId){
        List<Integer> list = roleMapper.getRoleByUserId(userId);
        return Result.builder().code("200").data(list).build();
    }
}
